package com.itboye.textileexpres.order;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.OrderDeltilteBeans;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.MainApplication;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.WebActivity;
import com.itboye.textileexpres.login.LoginActivity;
import com.itboye.textileexpres.myset.ActivityCustomerService;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.PriceChange;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

/**
 * 
 * @author 交易完成
 * 
 */
public class OrderDetailActivityJiaoyiwancheng extends BaseActivity implements
		OnClickListener, Observer {
	TextView title;
	ImageView back, img_other;
	String type;
	MyOrderPresenter orderPresenter;
	OrderDeltilteBeans beans;
	String order_code;
	private TextView order_num;
	private TextView create_time;
	String addre;
	private TextView tvshouhuo, textPhone, textPadd, faburen, cole, tvyunfei,
			tvprice, tvcandi;

	private LinearLayout litouytDetait;

	// 传过来的
	private TextView tvTime, tvdanhao, tvkuaidi;
	private String str;
	private TextView shengyutime;
	// 查看物流
	private Button btnWuliu;
	RelativeLayout mobile_rel;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_order_yijingshouhuo;
	}

	@Override
	public void onMyCreate() {
		title.setText(getString(R.string.h32));
		img_other.setVisibility(View.GONE);
		order_code = getIntent().getStringExtra("order_code");
		System.out.println("order_code_code" + order_code);
		str = getIntent().getStringExtra("str");
		beans = new OrderDeltilteBeans();
		type = getIntent().getStringExtra("type");
		orderPresenter = new MyOrderPresenter(this);
		orderPresenter.getorder(IsMobileNumPhone.isUid(), order_code);
		tvTime.setText(TimeFormat.onTime(beans));
		onBtnWuLiu();
		mobile_rel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if ("".equals(IsMobileNumPhone.isUid())
						|| IsMobileNumPhone.isUid() == null) {
					Intent intent = new Intent(OrderDetailActivityJiaoyiwancheng.this,
							LoginActivity.class);
					startActivity(intent);
				} else {
					Intent intent = new Intent(OrderDetailActivityJiaoyiwancheng.this,
							ActivityCustomerService.class);
					startActivity(intent);
				}

			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == orderPresenter.Xiang_success) {
				beans = (OrderDeltilteBeans) entity.getData();
				System.out.println("notenote"
						+ beans.getItems().get(0).getName());
				getCounte(beans);
				onAddres(beans);
			} else if (entity.getEventType() == orderPresenter.Delete_success) {
				toast(entity.getData() + "");

			}
		}
	}

	

	private void onAddres(OrderDeltilteBeans beans) {
		if (beans.get_address() == null) {
			return;
		} else {

			addre = beans.get_address().getCountry()
					+ beans.get_address().getProvince()
					+ beans.get_address().getDetailinfo();
			tvshouhuo.setText(beans.getContactname());
			textPhone.setText(beans.get_address().getMobile());
			textPadd.setText(addre);

			System.out.println("time" + beans.get_auto_op_time());

		}
	}

	private void getCounte(OrderDeltilteBeans beans) {
		System.out.println("LLLLLLLLLLLLLLLLL" + beans.getItems().size());
		for (int i = 0; i < beans.getItems().size(); i++) {

			if (beans.getContactname() == null) {
//				toast("数据为空");
			} else {
				// onSstartStr();
				faburen.setText(getString(R.string.sender)+":" + beans.getPublisher_name());

				order_num.setText(order_code);
				tvkuaidi.setText(beans.get_express().getExpressname());
				tvdanhao.setText("单号" + beans.get_express().getExpressno());
				System.out.println("LLLLLLLLLLLLLLLLLLLLL"
						+ beans.get_express().getExpressno());
				System.out.println("beansbeans" + beans.getItems().size());
				View view = LayoutInflater.from(
						OrderDetailActivityJiaoyiwancheng.this).inflate(
						R.layout.item_order_detail, null);
				TextView goods_name1_tv = (TextView) view
						.findViewById(R.id.goods_name1_tv);
				TextView goods_name2_tv = (TextView) view
						.findViewById(R.id.goods_name2_tv);
				TextView goods_detail_tv = (TextView) view
						.findViewById(R.id.goods_detail_tv);
				TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
				TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
				TextView num_tv = (TextView) view.findViewById(R.id.num_tv);

				goods_name1_tv.setText(beans.getPublisher_name());
				goods_name2_tv.setText(beans.getItems().get(i).getName());
				goods_detail_tv.setText(beans.getPublisher_name());
				color_tv.setText(beans.getItems().get(i).getSku_desc());
				price_tv.setText(PriceChange.Change(beans.getItems().get(i).getPrice()));
				num_tv.setText(beans.getItems().get(i).getCount());

				ImageView goods_img = (ImageView) view
						.findViewById(R.id.goods_img);
				create_time
						.setText(TimeFormat.DateFormat(beans.getCreatetime()));
//				tvyunfei.setText(beans.getItems().get(i).getOri_price() + "元");
//				tvprice.setText(beans.getItems().get(i).getDt_origin_country());
				tvyunfei.setText(PriceChange.Change(beans.getPost_price()) + getString(R.string.element));
				tvprice.setText(PriceChange.Change(beans.getPrice())+getString(R.string.element));
				XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
						+ beans.getItems().get(i).getImg(), goods_img);
				MarginLayoutParams layoutParams = new MarginLayoutParams(
						MarginLayoutParams.WRAP_CONTENT,
						MarginLayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(5, 5, 5, 5);
				litouytDetait.addView(view, layoutParams);

			}
		}
	}

	/***
	 * 查看物流
	 */
	public void onBtnWuLiu() {
		btnWuliu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainApplication.context,
						WebActivity.class);
				intent.putExtra(
						"url",
						"http://api.guannan.itboye.com/public/web.php/Express/index?order_code="
								+ beans.getOrder_code()
								+ IsMobileNumPhone.isUid()
								+ IsMobileNumPhone.isPasswrd());
				intent.putExtra("title", getString(R.string.h1192));
				startActivity(intent);
			}
		});
	}

}
